package edu.cmu.cs.webapp.ioms.ui.wizards.order;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import edu.cmu.cs.webapp.ioms.models.Client;
import edu.cmu.cs.webapp.ioms.utils.IOMSConstants;

/**
 * 
 * @author Christian
 */
public class CustomerWizardPage extends WizardPage {

	// private Text custIDTxt;
	private Text lnameTxt;
	private Text fnameTxt;
	private Text emailTxt;
	private Text phoneTxt;
	private Text companyTxt;
	private Text addressTxt;
	private Text cityTxt;
	private Combo stateComb;
	private Text countryTxt;

	public CustomerWizardPage(String pageName) {
		super(pageName);
		setTitle("Customer Information");
		setDescription("Input the necessary information of the new customer. \nNotice: All fields with asterisk(*) are required");

	}

	public CustomerWizardPage(String pageName, String title,
			ImageDescriptor titleImage) {
		super(pageName, title, titleImage);
	}

	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);

		layout.numColumns = 3;
		// layout.verticalSpacing = 9;

		/* id */
		//		label.setText("Customer ID*:"); //$NON-NLS-1$
		//
		// custIDTxt = new Text(container, SWT.BORDER | SWT.SINGLE);
		//
		// GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		// // _memberIDTxt.setEditable(false);
		// custIDTxt.setLayoutData(gd);
		// custIDTxt.addModifyListener(new ModifyListener() {
		// public void modifyText(ModifyEvent e) {
		// validate();
		// }
		// });

		// this.custIDTxt.setEditable(false);

		// first name
		Label label = new Label(container, SWT.NULL);
		label.setText("First Name*:"); //$NON-NLS-1$
		fnameTxt = new Text(container, SWT.BORDER);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		fnameTxt.setLayoutData(gd);

		// last name
		label = new Label(container, SWT.NULL);
		label.setText("Last Name*:"); //$NON-NLS-1$
		lnameTxt = new Text(container, SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		lnameTxt.setLayoutData(gd);

		// phone
		label = new Label(container, SWT.NULL);
		label.setText("Phone*:"); //$NON-NLS-1$

		phoneTxt = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		phoneTxt.setLayoutData(gd);
		phoneTxt.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});

		createLine(container, layout.numColumns);

		// email
		label = new Label(container, SWT.NULL);
		label.setText("Email:"); //$NON-NLS-1$
		emailTxt = new Text(container, SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		emailTxt.setLayoutData(gd);

		// company
		label = new Label(container, SWT.NULL);
		label.setText("Company:"); //$NON-NLS-1$

		companyTxt = new Text(container, SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		companyTxt.setLayoutData(gd);
		gd.horizontalSpan = 2;
		companyTxt.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});

		// address
		label = new Label(container, SWT.NULL);
		label.setText("Address:"); //$NON-NLS-1$

		addressTxt = new Text(container, SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		addressTxt.setLayoutData(gd);
		addressTxt.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});

		// city
		label = new Label(container, SWT.NULL);
		label.setText("City:"); //$NON-NLS-1$

		cityTxt = new Text(container, SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		cityTxt.setLayoutData(gd);
		cityTxt.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});

		// state
		label = new Label(container, SWT.NULL);
		label.setText("State:"); //$NON-NLS-1$

		stateComb = new Combo(container, SWT.BORDER | SWT.READ_ONLY);
		stateComb.setItems(IOMSConstants.US_STATES);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		stateComb.setLayoutData(gd);
		stateComb.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});

		// country
		label = new Label(container, SWT.NULL);
		label.setText("Country:"); //$NON-NLS-1$

		countryTxt = new Text(container, SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		countryTxt.setLayoutData(gd);
		countryTxt.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});

		if (isUpdate()) {
			loadFromExistingModel();
		}

		setControl(container);
		validate();
		container.pack(true);

	}

	private void loadFromExistingModel() {
		Client model = getModel();
		if (model != null) {
			this.fnameTxt.setText(model.getFname());
			this.lnameTxt.setText(model.getLname());
			this.phoneTxt.setText(model.getPhone());
			this.emailTxt.setText(model.getEmail() == null ? "" : model
					.getEmail());
			this.addressTxt.setText(model.getAddress() == null ? "" : model
					.getAddress());
			this.cityTxt
					.setText(model.getCity() == null ? "" : model.getCity());
			this.stateComb.setText(model.getState() == null ? "" : model
					.getState());
			this.countryTxt.setText(model.getCountry() == null ? "" : model
					.getCountry());
		}

	}

	private void createLine(Composite parent, int ncol) {
		Label line = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL
				| SWT.BOLD);
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = ncol;
		line.setLayoutData(gridData);
	}

	private boolean validate() {
		boolean valid = true;
		String errorMessage = null;

		if (valid && (getFname() == null || getFname().length() == 0)) {
			valid = false;
			errorMessage = "First name field could not be empty";
		}

		if (valid && (getLname() == null || getLname().length() == 0)) {
			valid = false;
			errorMessage = "Last name field could not be empty";
		}

		if (valid && (getPhone() == null || getPhone().length() == 0)) {
			valid = false;
			errorMessage = "Phone field could not be empty";
		}

		if (valid) {
			setErrorMessage(null);
			setPageComplete(true);
		} else {
			setErrorMessage(errorMessage);
			setPageComplete(false);
		}

		return valid;

	}

	public String getLname() {
		return lnameTxt.getText();
	}

	public String getFname() {
		return fnameTxt.getText();
	}

	public String getEmail() {
		return emailTxt.getText();
	}

	public String getPhone() {
		return phoneTxt.getText();
	}

	public String getCompany() {
		return companyTxt.getText();
	}

	public String getAddress() {
		return addressTxt.getText();
	}

	public String getCity() {
		return cityTxt.getText();
	}

	public String getState() {
		return stateComb.getText();
	}

	public String getCountry() {
		return countryTxt.getText();
	}

	private boolean isUpdate() {
		return ((CustomerWizard) getWizard()).isUpdate();
	}

	private Client getModel() {
		return ((CustomerWizard) getWizard()).getModel();
	}

}
